﻿using Newtonsoft.Json;
using Oplug.CoolTower.Filters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oplug.CoolTower
{
    public partial class FormConfig : MetroFramework.Forms.MetroForm
    {
        private DTO.SysConfig config;
        Utilities utility;
        public FormConfig()
        {
            InitializeComponent();
        }
        [Description("系统设置窗体加载")]
        private void FormConfig_Load(object sender, EventArgs e)
        {
            utility = new Utilities();
            config = JsonConvert.DeserializeObject<DTO.SysConfig>(utility.TxtRead(Utilities.cfgpath));
            if (config == null) {
                config = new DTO.SysConfig();
            }
            textBoxCompany.Text = config.Company;
            textBoxUpdateURL.Text = config.AutoUpdateURL;
            textBoxVersion.Text = Application.ProductVersion;
            DBIP.Text = config.DBIP;
            DBUser.Text = config.DBUser;
            DBPassword.Text = config.DBPsw;
            DBPort.Value = config.DBPort;
            DBName.Text = config.Database;

        }
        [Description("保存系统配置")]
        private void toolBtn_save_Click(object sender, EventArgs e)
        {
            //if (!MethodFilters.IsDeny(MethodBase.GetCurrentMethod().Name, Name, config.DBConn))
            //{
               
            //}
            try
            {
                config.Company = textBoxCompany.Text;
                config.Version = Application.ProductVersion;
                config.AutoUpdateURL = textBoxUpdateURL.Text;
                config.DBIP = DBIP.Text;
                config.DBPort = Convert.ToInt32(DBPort.Value);
                config.DBUser = DBUser.Text;
                config.DBPsw = DBPassword.Text;
                config.Database = DBName.Text;

                string contents = JsonConvert.SerializeObject(config);
                utility.TxtWrite(Utilities.cfgpath, contents);
                if (DialogResult.OK == Utilities.Confirm("设置成功，是否重新启动软件？"))
                {
                    System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                Utilities.Message(ex.Message, InfoType.Error);
            }
        }

        private void textBoxUpdateURL_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.InitialDirectory = "c:\\";
            if (file.ShowDialog() == DialogResult.OK)
            {
                this.textBoxUpdateURL.Text = file.FileName;
            }
        }

        private void textBoxDMTURL_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.InitialDirectory = "c:\\";
            if (file.ShowDialog() == DialogResult.OK)
            {
                this.DBIP.Text = file.FileName;
            }
        }

        private void toolBtn_reload_Click(object sender, EventArgs e)
        {
            textBoxCompany.Text = config.Company;
            textBoxUpdateURL.Text = config.AutoUpdateURL;
            textBoxVersion.Text = Application.ProductVersion;
            DBIP.Text = config.DBIP;
            DBUser.Text = config.DBUser;
            DBPassword.Text = config.DBPsw;
            DBPort.Value = config.DBPort;
            DBName.Text = config.Database;
        }

        private void toolBtn_testConnect_Click(object sender, EventArgs e)
        {
            using (Services.OplugLiveService Serv = new Services.OplugLiveService(config.DBConn))
            {
                try
                {
                    Serv.dbContext.Database.Connection.Open();
                    if (Serv.dbContext.Database.Connection.State == ConnectionState.Open)
                        MessageBox.Show("连接成功！", "提示");
                    else
                        MessageBox.Show("连接失败！", "提示");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("连接失败！发生错误：" + ex.Message, "提示");
                }
            }
        }

    }
}
